#ifndef __PLATFORM_PROTOCOL_H__
#define __PLATFORM_PROTOCOL_H__

#include <stdint.h>
#include "radar_protocol.h"

#define PLATFORM_PROTOCOL_HG  // 慧感和南通协议不一样的地方

#define PLATFORM_PROTOCOL_DATA_SIZE 2048
#define PLATFORM_PROTOCOL_MIX_SIZE  140

#define PLATFORM_PROTOCOL_HEADER 0x55AA
#define PLATFORM_PROTOCOL_TAIL   0xAA55

#define GO_LEFT     0x01
#define GO_STRAIGHT 0x02
#define GO_RIGHT    0x03

/*事件类型*/
#define ABNORMAL_STOP_CAR      0x1   // 异常停车 0x01
#define REVERSE_MOVE           0x2   // 逆行 0x02
#define BLOCK_I                0x3   // 拥堵 0x43
#define BLOCK_II               0x4   // 拥堵 0x41
#define QUNE_WAIT              0x5   // 缓行 0x42
#define OVERSPEED_OR_LOWSPEED  0x6   // 超高/低速检测 0x03,0x06
#define VEHICLE_LEAVE_AREA     0x7   // 车辆驶离区域 0x15
#define EMERGEMCY_LANE         0x8   // 占用应急车道 0x11
#define PERSON_DETECTION       0x9   // 行人检测 0x21
#define VEHICLE_TYPE_DETECTION 0x0A  // 车型检测
#define LICENCE_PLATE_RECOG    0x0B  // 车牌检测
#define LANE_CHANGE            0x0C  // 违章变道 0x13
#define SPILL                  0x0D  // 抛洒物
#define EMER_STOP              0x0E  // 紧急停车 0x08
#define ILL_PARTING            0x0F  // 违法停车 0x12
#define COMPRE_WIRE            0x10  // 压线 0x05
#define ABNOR_CHANGE           0x11  // 异常变道 0x04
#define ACCELERATION           0x12  // 加速 0x14
#define VEHICLE_COLLISION      0x13  // 碰撞 0x09

typedef struct __attribute__((packed)) {
    uint16_t header;
    uint8_t dev_id[10];
    uint8_t time_stamp[6];
    uint8_t type;
    uint8_t data[PLATFORM_PROTOCOL_DATA_SIZE];
    uint16_t tail;
} platform_protocol_t;

typedef struct __attribute__((packed)) {
    uint32_t frame_id;
    uint16_t radar_num;
    uint16_t video_num;
    uint16_t mix_num;
    uint8_t mix_data[PLATFORM_PROTOCOL_DATA_SIZE - 10];
} platform_protocol_mix_t;

typedef struct __attribute__((packed)) {
    uint8_t id[5];
    uint8_t type;
    uint8_t lane_id;
    uint8_t lane_type;
    int16_t position_x;
    int16_t position_y;
    int16_t speed;
    uint8_t length;
    int64_t longitude;
    int64_t latitude;
    uint8_t wheel_num;
#ifdef PLATFORM_PROTOCOL_HG
    uint16_t toll_vehicle_type;
#endif
    uint8_t plate_color;
    uint8_t plate_length;
    uint8_t car_color_length;
#ifdef PLATFORM_PROTOCOL_HG
    uint8_t guid_length;
#else
    uint8_t car_direction_length;
#endif
    uint8_t master_type_length;
    uint8_t slave_type_length;
    char plate[12];
    char car_color;
#ifdef PLATFORM_PROTOCOL_HG
    char guid[20];
#endif
} platform_protocol_mix_data_t;

typedef struct __attribute__((packed)) {
    uint8_t dev_id[10];
    uint8_t time_stamp[6];
    uint16_t id;
    uint8_t structured_data_flag;
    uint8_t type;
    uint8_t lane_id;
    uint8_t lane_type;
    int16_t position_x;
    int16_t position_y;
    int16_t speed;
    uint8_t length;
    int64_t longitude;
    int64_t latitude;
    int64_t speed_longitude;
    int64_t speed_latitude;
    uint8_t wheel_num;
    uint16_t toll_vehicle_type;
    uint8_t plate_color;
    uint8_t car_color;
    uint8_t plate_length;
    char plate[12];
    uint8_t guid_length;
    char guid[20];
    uint8_t master_type_length;
    uint8_t slave_type_length;
} platform_protocol_pole_relay_data_t;

typedef struct __attribute__((packed)) {
    uint8_t lane_id;
    uint8_t lane_name;
    uint16_t cycle;
    uint16_t small_car_num;
    uint16_t middle_car_num;
    uint16_t big_car_num;
    uint16_t reserved[8];
    uint16_t avg_time_use_rate;
    uint16_t avg_space_use_rate;
    uint16_t avg_speed;
    uint16_t avg_dis_time;
} platform_protocol_lane_t;

typedef struct __attribute__((packed)) {
    uint16_t lane_num;
    platform_protocol_lane_t lane[0];
} platform_protocol_static_t;

typedef struct __attribute__((packed)) {
    uint8_t event_type;
    uint8_t time_stamp[6];
    uint16_t dist_x;
    uint16_t dist_y;
    uint8_t is_single;
    int8_t licence[16];
    uint8_t obj_id[5];
    uint8_t obj_type;
    int8_t obj_color[8];
    uint16_t obj_speed;
    uint8_t lane_num;
    uint8_t traffic_jam[0];
} platform_protocol_event_t;

typedef struct __attribute__((packed)) {
    uint16_t avg_speed;
    int32_t traffic_jam_start;
    int32_t traffic_jam_end;
    int32_t traffic_jam_length;
    int32_t traffic_jam_start_longitude;
    int32_t traffic_jam_start_latitude;
    int32_t traffic_jam_end_longitude;
    int32_t traffic_jam_end_latitude;
} traffic_jam_t;

typedef struct __attribute__((packed)) {
    int16_t traffic_jam_length;
    int8_t congestion_level;
    int8_t is_overflow;
} traffic_jam2_t;

typedef struct __attribute__((packed)) {
    uint8_t device_id[10];
    uint8_t device_direction;
    int16_t offset_distance;
    uint8_t device_status;
    uint16_t device_type;
    int16_t device_speed_limit;
    uint16_t device_serial_num;
    uint16_t device_spacing;
    int64_t device_longitude;
    int64_t device_latitude;
} platform_protocol_dev_info_t;

size_t pack_platform_realtime_data(char *buffer, platform_protocol_mix_data_t *mix_data, size_t mix_num, uint8_t dev_type);
size_t pack_platform_static_data(char *buffer, radar_packet_80_t *radar_data, uint8_t dev_type);
size_t pack_platform_event_data(char *buffer, radar_packet_84_t *radar_data, uint8_t *time_stamp, uint8_t dev_type);
size_t pack_platform_relay_data(char *buffer, platform_protocol_pole_relay_data_t *mix_data, size_t mix_num);
size_t pack_pole_relay_data(radar_obj_info_t *radar_obj_info, platform_protocol_pole_relay_data_t *mix_data, size_t mix_num, uint8_t dev_type);
void get_device_id(uint8_t *dev_id, uint8_t dev_type);
uint8_t plate_color_to_platform(int plate);
int way_type_to_platform(int way_type);

void send_image_to_platform(char *guid, char *image, uint8_t dev_type);

void analysis_platform_buffer(char *buffer, int length);
#endif  // !__PLATFORM_PROTOCOL_H__